﻿//
// Copyright (c) 2024 BPer
//

#ifndef BPERWEBRTC_BPERRTCRTCP_H_
#define BPERWEBRTC_BPERRTCRTCP_H_

#include <bperutil/BPerType.h>
#include <bperutil/buffer/BPerBuffer.h>

#define BPer_RtcpHeader_Length 4
// RTCP Packet Types, @see http://www.networksorcery.com/enp/protocol/rtcp.htm
typedef enum
{
    BPerRtcpType_fir = 192,
    BPerRtcpType_sr = 200,
    BPerRtcpType_rr = 201,
    BPerRtcpType_sdes = 202,
    BPerRtcpType_bye = 203,
    BPerRtcpType_app = 204,
    BPerRtcpType_rtpfb = 205,
    BPerRtcpType_psfb = 206,
    BPerRtcpType_xr = 207,
}BPerRtcpType;



// RTCP Header, @see http://tools.ietf.org/html/rfc3550#section-6.1
// @remark The header must be 4 bytes, which align with the max field size 2B.
typedef struct
{
	uint8_t version;
	uint8_t padding;
	uint8_t rc;
	uint8_t type;
	uint16_t length:16;
}BPerRtcpHeader;


typedef struct{
	 uint64_t ntp;
	 uint32_t rtp_ts;
	 uint32_t send_rtp_packets;
	 uint32_t send_rtp_bytes;
}BPerRtcpSR;

typedef struct
{
    uint32_t ssrc;
    uint8_t fraction_lost;
    uint32_t lost_packets;
    uint32_t highest_sn;
    uint32_t jitter;
    uint32_t lsr;
    uint32_t dlsr;
}BPerRtcpRB;


typedef struct{
	 uint32_t mediaSsrc;
	 uint32_t vsize;
	 uint32_t capacity;
	 uint16_t* nacks;

}BPerRtcpNack;
typedef struct{
    uint8_t pb;
    uint8_t payload_type;
    uint32_t mediaSsrc;
    int32_t nb_native_rpsi;
    char* native_rpsi;
}BPerRtcpRpsi;
typedef struct{
	uint16_t first;
	uint16_t number;
	uint8_t picture;
}BPerRtcpSli;
typedef struct{
    uint32_t ssrc;
    uint32_t mediaSsrc;
    int32_t payloadLen;
    int32_t nb_data;
    char* data;
    BPerRtcpRB* rb;
    BPerRtcpSR* sr;
    BPerRtcpNack *nack;
    BPerRtcpRpsi* rpsi;
    BPerRtcpSli* sli;
    char* appName;
    void* twcc;
    BPerRtcpHeader header;
    uint8_t payload[1500];
}BPerRtcpCommon;

int32_t bPer_decode_rtcpHeader(BPerRtcpHeader* header,char* buf);
int32_t bPer_encode_rtcpHeader(BPerRtcpHeader* header,BPerBuffer* buf);
#endif /* BPERWEBRTC_BPERRTCRTCP_H_ */
